package com.cm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.admin.entity.Admin;
import com.cm.admin.entity.dto.AdminKeyWordDTO;
import com.cm.admin.mapper.AdminMapper;
import com.cm.admin.service.AdminService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @类 名:AdminServiceImpl </br>
 * @描 述:<br>
 * @日 期:2020/9/21 17:12 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        List<Admin> admin= adminMapper.selectList(queryWrapper);
        if (admin.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insert(Admin admin) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",admin.getUsername());
        List<Admin> admins= adminMapper.selectList(queryWrapper);
        if(admins.size() > 0){
            return false;
        }
        adminMapper.insert(admin);
        return true;
    }

    @Override
    public boolean update(Admin admin) {
        if (adminMapper.updateById(admin) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        if (adminMapper.deleteById(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<Admin> getAdminList(AdminKeyWordDTO adminKeyWordDTO) {
        // 分页
        Page<Admin> page = new Page<>();
        adminKeyWordDTO.setCurrentPage((adminKeyWordDTO.getCurrentPage()-1)*adminKeyWordDTO.getPageSize());
        List<Admin> adminList=adminMapper.getAdminList(adminKeyWordDTO);
        page.setRecords(adminList);
        page.setCurrent(adminKeyWordDTO.getCurrentPage());
        page.setSize(adminKeyWordDTO.getPageSize());
        int count=adminMapper.getAdminTotal(adminKeyWordDTO);
        page.setTotal(count);
        return page;
    }
}
